package com.springmvc.controller;

import com.springmvc.entity.Pclist;
import com.springmvc.service.PclistService;
import com.springmvc.way.Msfile;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by 29047 on 2018/1/4.
 */
@Controller
public class pclistController {

    @Autowired
    private PclistService pclistService;
    @Autowired
    private HttpServletRequest request;

    //新增杰出个人/校友
    @RequestMapping(value = "/add/pc",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addNewPc(@RequestParam("pcmation") String pcmation, @RequestParam("pic1") MultipartFile file){
        JSONObject json = new JSONObject();
        JSONObject pc = JSONObject.fromObject(pcmation);
        Pclist pclist = new Pclist();
        Iterator it = pc.keys();
        while (it.hasNext()) {
            String key = it.next().toString();
            if (key.equals("name")) {
                pclist.setName(pc.getString(key));
            }
            if (key.equals("sex")) {
                pclist.setSex(Integer.parseInt(pc.getString(key)));
            }
            if (key.equals("origin")) {
                pclist.setOrigin(pc.getString(key));
            }
            if (key.equals("class_num")) {
                pclist.setClassNum(pc.getString(key));
            }
            if (key.equals("introduce")) {
                pclist.setIntroduce(pc.getString(key));
            }
            if (key.equals("profession")) {
                pclist.setProfession(pc.getString(key));
            }
            if (key.equals("type")) {
                pclist.setType(Integer.parseInt(pc.getString(key)));
            }
        }
        Msfile msfile = new Msfile(file,request);
        String pcPhoto = msfile.saveFile();
        if (pcPhoto != null){
            pclist.setPicture(pcPhoto);
        } else {
            pclist.setPicture("images/default.jpg");
        }

        if (pclistService.insert(pclist) == 1){
            json.put("result",1);
        } else {
            json.put("result",0);
        }
        return json;
    }


    //更新杰出个人/校友
    @RequestMapping(value = "/pc/update",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updatePc(@RequestParam("pcmation") String pcmation, @RequestParam("pic1") MultipartFile file){

        JSONObject json = new JSONObject();
        JSONObject pc = JSONObject.fromObject(pcmation);
        Pclist pclist = pclistService.selectByPrimaryKey(Integer.parseInt(pc.getString("id")));
        Iterator it = pc.keys();
        while (it.hasNext()) {
            String key = it.next().toString();
            if (key.equals("name")) {
                pclist.setName(pc.getString(key));
            }
            if (key.equals("sex")) {
                pclist.setSex(Integer.parseInt(pc.getString(key)));
            }
            if (key.equals("origin")) {
                pclist.setOrigin(pc.getString(key));
            }
            if (key.equals("class_num")) {
                pclist.setClassNum(pc.getString(key));
            }
            if (key.equals("introduce")) {
                pclist.setIntroduce(pc.getString(key));
            }
            if (key.equals("profession")) {
                pclist.setProfession(pc.getString(key));
            }
            if (key.equals("type")) {
                pclist.setType(Integer.parseInt(pc.getString(key)));
            }
        }
        Msfile msfile = new Msfile(file,request);
        String pcPhoto = msfile.saveFile();
        if (pcPhoto != null){
            pclist.setPicture(pcPhoto);
        } else {
            pclist.setPicture("images/default.jpg");
        }
        if (pclistService.updateByPrimaryKey(pclist) == 1){
            json.put("result",1);
        } else {
            json.put("result",0);
        }
        return json;
    }

    //获取单条信息详情
    @RequestMapping(value = "/pc/detail",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject detPc(@RequestParam("pcId") String pcId){
        JSONObject jsonObject = new JSONObject();
        Pclist pclist = pclistService.selectByPrimaryKey(Integer.parseInt(pcId));
        jsonObject.put("data",pclist);
        return jsonObject;
    }

    //删除单条信息
    @RequestMapping(value = "/del/pc",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delPc(@RequestParam("pcId") String pcId){
        JSONObject json = new JSONObject();
        if (pclistService.deleteByPrimaryKey(Integer.parseInt(pcId)) == 1){
            json.put("result",1);
        } else {
            json.put("result",0);
        }
        return json;
    }

    //根据type检索信息
    @RequestMapping(value = "/type/pc",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject typePc(@RequestParam("type") String type){
        JSONObject json = new JSONObject();
        ArrayList<Pclist> pclists = pclistService.selectByType(Integer.parseInt(type));
        json.put("data",pclists);
        return json;
    }

}
